/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yqlweb.persistencia;

import yqlweb.util.*;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import yqlweb.modelo.Foto;
import yqlweb.modelo.Clima;
import yqlweb.modelo.PerfilTwitter;

/**
 *
 * @author rodrigo
 */
public class Persistencia {

    @SuppressWarnings("empty-statement")
    public List<Foto> obterFotosPorDescricao(String descricao, int limite) {
        try {
            if (!descricao.trim().equals("")) {

                String sql = "select id,farm,server,secret,title from flickr.photos.search where text='" + descricao + "' limit " + limite + "";
                Querys query = new Querys();
                InputStream xml = query.getResponse(sql);
                Parse parse = new Parse();
                List<Foto> fotos = parse.xmlToObjectFotos(xml);
                return fotos;
            } else {
                return null;
            }
        } catch (Exception ex) {
            Logger.getLogger(Persistencia.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public Clima obterClimaPorLocalizacao(String local) {
        try {
            if (!local.trim().equals("")) {
                String sql = "Select rss.channel.item from weather.bylocation where location = '" + local + "' and unit = 'c'";

                Querys query = new Querys();
                InputStream xml = query.getResponse(sql);
                Parse parse = new Parse();
                Clima clima = parse.xmlToObjectClima(xml);
                return clima;
            } else {
                return null;
            }

        } catch (Exception ex) {
            Logger.getLogger(Persistencia.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public PerfilTwitter obterPerfilTwitter(String usuario) {
        try {
            if (!usuario.trim().equals("")) {
                String sql = "select name, screen_name, description, location, profile_image_url, followers_count, status.text from twitter.users where id = '" + usuario + "' ";

                Querys query = new Querys();
                InputStream xml = query.getResponse(sql);
                Parse parse = new Parse();
                PerfilTwitter perfil = parse.xmlToObjectPerfilTwitter(xml);
                return perfil;
            } else {
                return null;
            }

        } catch (Exception ex) {
            Logger.getLogger(Persistencia.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
 
}
